package com.innovation.chengzhangriji.ui.widget;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.text.ClipboardManager;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.Toast;

import com.innovation.chengzhangriji.Baby;
import com.innovation.chengzhangriji.ChZhRJApplication;
import com.innovation.chengzhangriji.Config;
import com.innovation.chengzhangriji.R;
import com.innovation.chengzhangriji.TimelinePhoto;
import com.innovation.chengzhangriji.database.DatabaseManager;
import com.innovation.chengzhangriji.http.BaseRequest;
import com.innovation.chengzhangriji.preferences.LoginInfo;
import com.innovation.chengzhangriji.ui.TimelineGalleryActivity;
import com.innovation.chengzhangriji.ui.TimelinePlayActivity;
import com.innovation.chengzhangriji.ui.model.UITimeline;
import com.innovation.chengzhangriji.ui.model.UITimelineAudio;
import com.innovation.chengzhangriji.ui.model.UITimelineMilestone;
import com.innovation.chengzhangriji.ui.model.UITimelinePhoto;
import com.innovation.chengzhangriji.ui.model.UITimelineText;
import com.innovation.chengzhangriji.ui.model.UITimelineVideo;
import com.innovation.chengzhangriji.util.WXUtils;
import com.orhanobut.dialogplus.DialogPlus;
import com.orhanobut.dialogplus.GridHolder;
import com.orhanobut.dialogplus.OnItemClickListener;
import com.tencent.mm.sdk.openapi.IWXAPI;

import java.io.IOException;
import java.util.ArrayList;

public class TimelineAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements TimelineViewHolder.OnLongClickTimelineListener, TimelinePhotoViewHolder.OnClickImageListener, TimelineAudioViewHolder.OnClickAudioListener, TimelineVideoViewHolder.OnClickVideoListener, View.OnClickListener {

    private static final float EDGE = 150.0f;

    private Context mContext;

    private ArrayList<UITimeline> timelineArrayList;

    private MediaPlayer mPlayer;

    private int audioPosition = -1;

    private int mPosition;

    private Dialog mDialog;

    CuteTextView textShare;

    FrameLayout layoutDriver;

    CuteTextView textDelete;

    FrameLayout layoutDriver2;

    CuteTextView textCopy;

    public TimelineAdapter(Context context) {
        mContext = context;
        timelineArrayList = new ArrayList<UITimeline>();

        mDialog = new Dialog(mContext, R.style.Theme_ChZhRJ_Dialog_TimeLine);
        mDialog.setContentView(R.layout.dialog_timeline);
        textShare = (CuteTextView) mDialog.getWindow().getDecorView().findViewById(R.id.text_share);
        textShare.setOnClickListener(this);
        layoutDriver = (FrameLayout) mDialog.getWindow().getDecorView().findViewById(R.id.layout_driver);
        layoutDriver2 = (FrameLayout) mDialog.getWindow().getDecorView().findViewById(R.id.layout_driver_2);
        textDelete = (CuteTextView) mDialog.getWindow().getDecorView().findViewById(R.id.text_delete);
        textDelete.setOnClickListener(this);
        textCopy = (CuteTextView) mDialog.getWindow().getDecorView().findViewById(R.id.text_copy);
        textCopy.setOnClickListener(this);
    }

    @Override
    public int getItemViewType(int position) {
        if (timelineArrayList != null) {
            return timelineArrayList.get(position).getType();
        }
        return -1;
    }

    @Override
    public int getItemCount() {
        if (timelineArrayList != null) {
            return timelineArrayList.size();
        }
        return 1;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view;
        TimelineViewHolder viewHolder = null;
        switch (viewType) {
            case UITimeline.TYPE_TEXT:
                view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_timeline, viewGroup, false);
                viewHolder = new TimelineTextViewHolder(mContext, view);
                break;
            case UITimeline.TYPE_PHOTO:
                view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_timeline, viewGroup, false);
                viewHolder = new TimelinePhotoViewHolder(mContext, view);
                ((TimelinePhotoViewHolder) viewHolder).setOnClickImageListener(this);
                break;
            case UITimeline.TYPE_AUDIO:
                view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_timeline, viewGroup, false);
                viewHolder = new TimelineAudioViewHolder(mContext, view);
                ((TimelineAudioViewHolder) viewHolder).setOnClickAudioListener(this);
                break;
            case UITimeline.TYPE_VIDEO:
                view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_timeline, viewGroup, false);
                viewHolder = new TimelineVideoViewHolder(mContext, view);
                ((TimelineVideoViewHolder) viewHolder).setOnClickVideoListener(this);
                break;
            case UITimeline.TYPE_MILESTONE:
                view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_timeline, viewGroup, false);
                viewHolder = new TimelineMilestoneViewHolder(mContext, view);
                break;
        }
        if (viewHolder != null) {
            viewHolder.setOnLongClickTimelineListener(this);
            return viewHolder;
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        switch (getItemViewType(position)) {
            case UITimeline.TYPE_TEXT:
                onBindTimelineTextViewHolder(holder, position);
                break;
            case UITimeline.TYPE_PHOTO:
                onBindTimelinePhotoViewHolder(holder, position);
                break;
            case UITimeline.TYPE_AUDIO:
                onBindTimelineAudioViewHolder(holder, position);
                break;
            case UITimeline.TYPE_VIDEO:
                onBindTimelineVideoViewHolder(holder, position);
                break;
            case UITimeline.TYPE_MILESTONE:
                onBindTimelineMilestoneViewHolder(holder, position);
                break;
        }
    }

    @Override
    public void onLongClickTimeline(final TimelineViewHolder holder) {
        mPosition = holder.getLayoutPosition();

        // 判断是否有权限分享和删除
        LoginInfo loginInfo = Config.getLoginInfo(mContext);
        if (loginInfo == null) {
            return;
        }
        // 默认显示
        textShare.setVisibility(View.VISIBLE);
        layoutDriver.setVisibility(View.VISIBLE);
        textDelete.setVisibility(View.VISIBLE);
        layoutDriver2.setVisibility(View.VISIBLE);

        if (holder instanceof TimelineAudioViewHolder || holder instanceof TimelineVideoViewHolder) {
            textShare.setVisibility(View.GONE);
            layoutDriver.setVisibility(View.GONE);
            textCopy.setVisibility(View.GONE);
            layoutDriver2.setVisibility(View.GONE);
        } else {
            textShare.setVisibility(View.VISIBLE);
            layoutDriver.setVisibility(View.VISIBLE);
            textCopy.setVisibility(View.VISIBLE);
            layoutDriver2.setVisibility(View.VISIBLE);
            if(holder instanceof TimelinePhotoViewHolder && TextUtils.isEmpty(((UITimelinePhoto) timelineArrayList.get(mPosition)).getText())) {
                textCopy.setVisibility(View.GONE);
                layoutDriver2.setVisibility(View.GONE);
            }
        }

        String userId = loginInfo.getUserID();
        int babyId = loginInfo.getBabyID();
        Baby baby = DatabaseManager.getInstance(mContext).queryBaby(userId, babyId);
        if (baby == null) {
            textShare.setVisibility(View.GONE);
            layoutDriver.setVisibility(View.GONE);
            textDelete.setVisibility(View.GONE);
            layoutDriver2.setVisibility(View.GONE);
            // return;
        }
        if (!timelineArrayList.get(mPosition).getRole().equals(baby.getParentName())) {
            textShare.setVisibility(View.GONE);
            layoutDriver.setVisibility(View.GONE);
            textDelete.setVisibility(View.GONE);
            layoutDriver2.setVisibility(View.GONE);
            // return;
        }
        if(textCopy.getVisibility() == View.GONE && textShare.getVisibility() == View.GONE && textDelete.getVisibility() == View.GONE) {
            return;
        }
        Window dialogWindow = mDialog.getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics dm = mContext.getResources().getDisplayMetrics();
        float pX = 35.5f * dm.density + holder.getX();
        float pY = (holder.itemView.getY() + 48 * dm.density + holder.getY());
        float sX = mContext.getResources().getDisplayMetrics().widthPixels / 2;
        float sY = mContext.getResources().getDisplayMetrics().heightPixels / 2;
        lp.x = (int) (pX - sX);
        lp.y = (int) (pY - sY);
        dialogWindow.setAttributes(lp);
        mDialog.show();
    }

    @Override
    public void onClickImage(TimelinePhotoViewHolder holder, int index) {
        Intent intent = new Intent(mContext, TimelineGalleryActivity.class);
        Bundle bundle = new Bundle();
        bundle.putInt("index", index);
        bundle.putStringArray("urls", ((UITimelinePhoto) timelineArrayList.get(holder.getLayoutPosition())).getLocalThumbUrls());
        intent.putExtras(bundle);
        mContext.startActivity(intent);
    }

    @Override
    public void onLongClickImage(TimelinePhotoViewHolder holder, int index) {
        onLongClickTimeline(holder);
    }

    @Override
    public void onClickAudio(TimelineAudioViewHolder holder) {
        UITimelineAudio uiTimelineAudio = ((UITimelineAudio) timelineArrayList.get(holder.getLayoutPosition()));
        if (uiTimelineAudio == null) {
            return;
        }
        if (TextUtils.isEmpty(uiTimelineAudio.getLocalUrl())) {
            return;
        }
        if (!uiTimelineAudio.isPlay()) {
            if (audioPosition != -1) {
                stopPlayback();
            }
            audioPosition = holder.getLayoutPosition();
            ((UITimelineAudio) timelineArrayList.get(audioPosition)).setPlay(true);
            notifyItemChanged(audioPosition);
            playAudio(uiTimelineAudio.getLocalUrl());
        } else {
            stopPlayback();
        }
    }

    @Override
    public void onLongClickAudio(TimelineAudioViewHolder holder) {
        onLongClickTimeline(holder);
    }

    @Override
    public void onClickVideo(TimelineVideoViewHolder holder) {
        int position = holder.getLayoutPosition();
        Intent intent = new Intent(mContext, TimelinePlayActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("uuid", timelineArrayList.get(holder.getLayoutPosition()).getUuid());
        bundle.putString("url", ((UITimelineVideo) timelineArrayList.get(position)).getUrl());
        intent.putExtras(bundle);
        mContext.startActivity(intent);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.text_share:
                // 判断是否已经上传完成
                if (DatabaseManager.getInstance(mContext).queryRemoteExist(timelineArrayList.get(mPosition).getUuid())) {
                    mDialog.dismiss();
                    openShareDialog();
                } else {
                    Toast.makeText(mContext, "日记正在上传,请稍后分享!", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.text_delete:
                mDialog.dismiss();
                openDeleteDialog();
                break;
            case R.id.text_copy:
                mDialog.dismiss();
                copyText();
                break;
        }
    }

    // 复制文本
    public void copyText() {
        String text = "";
        switch(getItemViewType(mPosition)) {
            case UITimeline.TYPE_TEXT:
                text = ((UITimelineText)timelineArrayList.get(mPosition)).getText();
                break;
            case UITimeline.TYPE_PHOTO:
                text = ((UITimelinePhoto)timelineArrayList.get(mPosition)).getText();
                break;
            case UITimeline.TYPE_MILESTONE:
                text = ((UITimelineMilestone)timelineArrayList.get(mPosition)).getMilestone();
                break;
        }
        if(!TextUtils.isEmpty(text)) {
            ClipboardManager cmb = (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE);
            cmb.setText(text);
        }
    }

    public void updateComplete(String uuid, String url, String localUrl) {
        for (int i = 0; i < timelineArrayList.size(); i++) {
            if (timelineArrayList.get(i).getUuid().equals(uuid)) {
                switch (timelineArrayList.get(i).getType()) {
                    case UITimeline.TYPE_PHOTO:
                        UITimelinePhoto uiTimelinePhoto = ((UITimelinePhoto) timelineArrayList.get(i));
                        int length = uiTimelinePhoto.getUrls().length;
                        String[] localUrls = uiTimelinePhoto.getLocalThumbUrls();
                        for (int j = 0; j < length; j++) {
                            if (uiTimelinePhoto.getUrls()[j].equals(url)) {
                                localUrls[j] = localUrl;
                                uiTimelinePhoto.setText(uiTimelinePhoto.getText());
                                uiTimelinePhoto.setTheme(uiTimelinePhoto.getTheme());
                                uiTimelinePhoto.setLocalThumbUrls(localUrls);
                                notifyItemChanged(i);
                            }
                        }
                        return;
                    case UITimeline.TYPE_AUDIO:
                        UITimelineAudio uiTimelineAudio = (UITimelineAudio) timelineArrayList.get(i);
                        uiTimelineAudio.setLocalUrl(localUrl);
                        notifyItemChanged(i);
                        return;
                    case UITimeline.TYPE_VIDEO:
                        UITimelineVideo uiTimelineVideo = (UITimelineVideo) timelineArrayList.get(i);
                        uiTimelineVideo.setLocalScreenshotUrl(localUrl);
                        notifyItemChanged(i);
                        break;
                }
            }
        }
    }

    public void clear() {
        audioPosition = -1;
        timelineArrayList.clear();
        notifyDataSetChanged();
    }

    public void addText(UITimelineText uiTimelineText) {
        stopPlayback();
        timelineArrayList.add(0, uiTimelineText);
        notifyDataSetChanged();
    }

    public void addTextDesc(UITimelineText uiTimelineText) {
        stopPlayback();
        timelineArrayList.add(uiTimelineText);
        notifyDataSetChanged();
    }

    public void addPhoto(UITimelinePhoto uiTimelinePhoto) {
        stopPlayback();
        timelineArrayList.add(0, uiTimelinePhoto);
        notifyDataSetChanged();
    }

    public void addPhotoDesc(UITimelinePhoto uiTimelinePhoto) {
        stopPlayback();
        timelineArrayList.add(uiTimelinePhoto);
        notifyDataSetChanged();
    }

    public void addAudio(UITimelineAudio uiTimelineAudio) {
        stopPlayback();
        timelineArrayList.add(0, uiTimelineAudio);
        notifyDataSetChanged();
    }

    public void addAudioDesc(UITimelineAudio uiTimelineAudio) {
        stopPlayback();
        timelineArrayList.add(uiTimelineAudio);
        notifyDataSetChanged();
    }

    public void addVideo(UITimelineVideo uiTimelineVideo) {
        stopPlayback();
        timelineArrayList.add(0, uiTimelineVideo);
        notifyDataSetChanged();
    }

    public void addVideoDesc(UITimelineVideo uiTimelineVideo) {
        stopPlayback();
        timelineArrayList.add(uiTimelineVideo);
        notifyDataSetChanged();
    }

    public void addMilestone(UITimelineMilestone uiTimelineMilestone) {
        stopPlayback();
        timelineArrayList.add(0, uiTimelineMilestone);
        notifyDataSetChanged();
    }

    public void addMilestoneDesc(UITimelineMilestone uiTimelineMilestone) {
        stopPlayback();
        timelineArrayList.add(uiTimelineMilestone);
        notifyDataSetChanged();
    }


    public String getLastUUID() {
        return timelineArrayList.get(timelineArrayList.size() - 1).getUuid();
    }

    public long getLastCreateTS() {
        return timelineArrayList.get(timelineArrayList.size() - 1).getLocalTime();
    }

    public int getLength() {
        if (timelineArrayList != null) {
            return timelineArrayList.size();
        }
        return 0;
    }

    public void stopPlayback() {
        if (audioPosition != -1) {
            ((UITimelineAudio) timelineArrayList.get(audioPosition)).setPlay(false);
            notifyItemChanged(audioPosition);
            audioPosition = -1;
        }
        if (mPlayer == null) // we were not in playback
            return;
        mPlayer.stop();
        mPlayer.release();
        mPlayer = null;
    }

    private void onBindTimelineTextViewHolder(RecyclerView.ViewHolder holder, int position) {
        TimelineTextViewHolder viewHolder = ((TimelineTextViewHolder) holder);
        String text = ((UITimelineText) timelineArrayList.get(position)).getText();
        viewHolder.setText(text);
        onBindTimelineViewHolder(viewHolder, position);
    }

    private void onBindTimelinePhotoViewHolder(RecyclerView.ViewHolder holder, int position) {
        TimelinePhotoViewHolder viewHolder = ((TimelinePhotoViewHolder) holder);
        String photoTheme = ((UITimelinePhoto) timelineArrayList.get(position)).getTheme();
        String photoText = ((UITimelinePhoto) timelineArrayList.get(position)).getText();
        String[] localThumbUrls = ((UITimelinePhoto) timelineArrayList.get(position)).getLocalThumbUrls();
        viewHolder.setPhotoGroup(localThumbUrls);
        viewHolder.setThemeAndText(photoTheme, photoText);
        onBindTimelineViewHolder(viewHolder, position);
    }

    private void onBindTimelineAudioViewHolder(RecyclerView.ViewHolder holder, int position) {
        TimelineAudioViewHolder viewHolder = ((TimelineAudioViewHolder) holder);
        int length = ((UITimelineAudio) timelineArrayList.get(position)).getLength();
        viewHolder.setLength(length);
        if (((UITimelineAudio) timelineArrayList.get(position)).isPlay()) {
            viewHolder.play();
        } else {
            viewHolder.stop();
        }
        if (TextUtils.isEmpty(((UITimelineAudio) timelineArrayList.get(position)).getLocalUrl())) {
            viewHolder.notReady();
        } else {
            viewHolder.ready();
        }
        onBindTimelineViewHolder(viewHolder, position);
    }

    private void onBindTimelineVideoViewHolder(RecyclerView.ViewHolder holder, int position) {
        TimelineVideoViewHolder viewHolder = ((TimelineVideoViewHolder) holder);
        String screenshotUrl = ((UITimelineVideo) timelineArrayList.get(position)).getLocalScreenshotUrl();
        int progress = ((UITimelineVideo) timelineArrayList.get(position)).getProgress();
        boolean show = ((UITimelineVideo) timelineArrayList.get(position)).isProgress();
        viewHolder.setScreenshotUrl(screenshotUrl);
        viewHolder.setProgressShow(show);
        viewHolder.setProgress(progress);
        onBindTimelineViewHolder(viewHolder, position);
    }

    private void onBindTimelineMilestoneViewHolder(RecyclerView.ViewHolder holder, int position) {
        TimelineMilestoneViewHolder viewHolder = ((TimelineMilestoneViewHolder) holder);
        String milestone = ((UITimelineMilestone) timelineArrayList.get(position)).getMilestone();
        viewHolder.setMilestone(milestone);
        onBindTimelineViewHolder(viewHolder, position);
    }

    private void onBindTimelineViewHolder(TimelineViewHolder viewHolder, int position) {
        viewHolder.setDate(timelineArrayList.get(position).getDate());
        viewHolder.setCount(timelineArrayList.get(position).getCount());
        viewHolder.setRole(timelineArrayList.get(position).getRole());
        viewHolder.setTime(timelineArrayList.get(position).getTime());

        // 判断与上一条数据是否为同一天
        if (position == 0) {
            viewHolder.setDateVisibility(true, true);
        } else {
            if (timelineArrayList.get(position).getDate().equals(timelineArrayList.get(position - 1).getDate())) {
                viewHolder.setDateVisibility(false, false);
            } else {
                viewHolder.setDateVisibility(true, false);
            }
            if (timelineArrayList.get(position).getTime().equals(timelineArrayList.get(position - 1).getTime())
                    && timelineArrayList.get(position).getRole().equals(timelineArrayList.get(position - 1).getRole())) {
                viewHolder.setTimeVisibility(false);
            } else {
                viewHolder.setTimeVisibility(true);
            }
        }
    }

    private void sendToWXSession(int position) {
        IWXAPI api = ((ChZhRJApplication) mContext.getApplicationContext()).getWeiXinApi();
        String uuid = timelineArrayList.get(position).getUuid();
        switch (getItemViewType(position)) {
            case UITimeline.TYPE_TEXT:
                WXUtils.sendTextToWXSession(api, uuid, "成长日记,可以随身携带的日记本,给宝宝制定的专属成人礼", ((UITimelineText) timelineArrayList.get(position)).getText());
                break;
            case UITimeline.TYPE_PHOTO:
                String thumbUrl = ((UITimelinePhoto) timelineArrayList.get(position)).getLocalThumbUrls()[0];
                Bitmap thumb = decodeFile(thumbUrl);
                WXUtils.sendUrlToWXSession(api, uuid, "成长日记,可以随身携带的日记本,给宝宝制定的专属成人礼", buildDescription(position), BaseRequest.PATH + "/wxshare/share/" + uuid, thumb);
                break;
            case UITimeline.TYPE_MILESTONE:
                WXUtils.sendTextToWXSession(api, uuid, "成长日记,可以随身携带的日记本,给宝宝制定的专属成人礼", ((UITimelineMilestone) timelineArrayList.get(position)).getMilestone());
                break;
        }
    }

    private void sendToWXTimeline(int position) {
        IWXAPI api = ((ChZhRJApplication) mContext.getApplicationContext()).getWeiXinApi();
        String uuid = timelineArrayList.get(position).getUuid();
        switch (getItemViewType(position)) {
            case UITimeline.TYPE_TEXT:
                WXUtils.sendTextToWXTimeline(api, uuid, "成长日记,可以随身携带的日记本,给宝宝制定的专属成人礼", ((UITimelineText) timelineArrayList.get(position)).getText());
                break;
            case UITimeline.TYPE_PHOTO:
                String thumbUrl = ((UITimelinePhoto) timelineArrayList.get(position)).getLocalThumbUrls()[0];
                Bitmap thumb = decodeFile(thumbUrl);
                WXUtils.sendUrlToWXTimeline(api, uuid, buildDescription(position), BaseRequest.PATH + "/WXShare/Share/" + uuid, thumb);
                break;
            case UITimeline.TYPE_MILESTONE:
                WXUtils.sendTextToWXTimeline(api, uuid, "成长日记,可以随身携带的日记本,给宝宝制定的专属成人礼", ((UITimelineMilestone) timelineArrayList.get(position)).getMilestone());
                break;
        }
    }

    private String buildDescription(int position) {
        String theme = ((UITimelinePhoto) timelineArrayList.get(position)).getTheme();
        if (TextUtils.isEmpty(theme)) {
            theme = "";
        } else {
            theme = "(" + theme + ")";
        }
        String text = ((UITimelinePhoto) timelineArrayList.get(position)).getText();
        if (TextUtils.isEmpty(text)) {
            text = "我给宝宝记录了一篇成长日记,大家快来围观吧!";
        }
        return theme + text;
    }

    private void playAudio(final String path) {
        mPlayer = new MediaPlayer();
        try {
            mPlayer.setDataSource(path);
            mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    stopPlayback();
                }
            });
            mPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
                @Override
                public boolean onError(MediaPlayer mp, int what, int extra) {
                    stopPlayback();
                    return true;
                }
            });
            mPlayer.prepare();
            mPlayer.start();
        } catch (IOException e) {
            e.printStackTrace();
            stopPlayback();
            mPlayer = null;
        }
    }

    private void openDeleteDialog() {
        new AlertDialog.Builder(mContext)
                .setTitle("确认要删除么?")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // 更新界面
                        DatabaseManager.getInstance(mContext).deleteUnSyncTimeline(timelineArrayList.get(mPosition).getUuid());
                        stopPlayback();
                        timelineArrayList.remove(mPosition);
                        notifyItemRemoved(mPosition);
                        notifyItemChanged(mPosition);
                        Config.startDeleteTimelineService(mContext);
                    }
                })
                .setNegativeButton("取消", null)
                .show();
    }

    private void openShareDialog() {
        DialogPlus dialog = new DialogPlus.Builder(mContext)
                .setAdapter(new WXAdapter())
                .setGravity(Gravity.BOTTOM)
                .setContentHolder(new GridHolder(2))
                .setCancelable(true)
                .setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(DialogPlus dialog, Object item, View view, int position) {
                        switch (position) {
                            case 0:
                                sendToWXSession(mPosition);
                                dialog.dismiss();
                                break;
                            case 1:
                                sendToWXTimeline(mPosition);
                                dialog.dismiss();
                                break;
                        }
                    }
                })
                .create();
        dialog.show();
    }

    private class WXAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return 2;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder;
            View view = convertView;

            if (view == null) {
                view = LayoutInflater.from(mContext).inflate(R.layout.item_wx, parent, false);
                viewHolder = new ViewHolder();
                viewHolder.textView = (CuteTextView) view.findViewById(R.id.text_view);
                viewHolder.imageView = (ImageView) view.findViewById(R.id.image_view);
                view.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) view.getTag();
            }
            switch (position) {
                case 0:
                    viewHolder.textView.setText("分享到微信");
                    viewHolder.imageView.setImageResource(R.drawable.ic_wechat_session);
                    break;
                case 1:
                    viewHolder.textView.setText("分享到朋友圈");
                    viewHolder.imageView.setImageResource(R.drawable.ic_wechat_timeline);
                    break;
            }

            return view;
        }

        class ViewHolder {
            CuteTextView textView;
            ImageView imageView;
        }
    }

    private Bitmap decodeFile(String path) {
        BitmapFactory.Options decodeOptions = new BitmapFactory.Options();
        decodeOptions.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, decodeOptions);
        int width = decodeOptions.outWidth;
        int height = decodeOptions.outHeight;
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = false;
        int size = 1;
        if (width > EDGE || height > EDGE) {
            int wRatio = Math.round(width / EDGE);
            int hRation = Math.round(height / EDGE);
            size = wRatio < hRation ? wRatio : hRation;
        }
        options.inSampleSize = size;
        return BitmapFactory.decodeFile(path, options);
    }
}